﻿using System;
using Moonlite.ExtensionApi.XimlInterfaces;

namespace Moonlite.Platform.Ximl
{
    public class BarNodeDataProvider : INodeDataProvider
    {
        bool INodeDataProvider.ParseXimlNode(ref IXimlNode node, out DescriptorBase descriptor)
        {
            descriptor = null;
            if (node.Name == "Bar")
            {
                bool reference = node["guid"].StartsWith("{") && node["guid"].EndsWith("}");
                if (string.IsNullOrEmpty(node["guid"]))
                {
                    throw new XimlInvalidElementException("The Bar Ximl element must have attribute guid.");
                }
                if (!reference && string.IsNullOrEmpty(node["type"]))
                {
                    throw new XimlInvalidElementException("The Bar Ximl element must have attribute type.");
                }
                if (!reference && string.IsNullOrEmpty(node["name"]))
                {
                    throw new XimlInvalidElementException("The Bar Ximl element must have attribute name.");
                }
                DescriptorBase parent = (DescriptorBase)node.Parent.Tag;
                if (!reference)
                {
                    descriptor = new BarDescriptor(node, parent, node["guid"]);
                    node["name"] = ResourceManager.ConvertReferenceToString(node["name"]);
                    ((BarDescriptor)descriptor).Name = node["name"];
                    BarType type = BarType.Menu;
                    if (!Enum.TryParse<BarType>(node["type"], out type))
                    {
                        throw new XimlInvalidElementException("The type attribute of Ximl element BarItem must have one of the following values: " + Enum.GetValues(typeof(BarType)).ToString());
                    }
                    ((BarDescriptor)descriptor).Type = type;
                    if (!XimlTree.Instance.IsNativeElement(node["guid"]))
                    {
                        XimlTree.Instance.Descriptors.Add(node["guid"], descriptor);
                    }
                }
                else
                {
                    node["guid"] = node["guid"].Trim(new char[] { '{', '}' });
                    node["guid"] = ResourceManager.ConvertReferenceToString(node["guid"]);
                    descriptor = XimlTree.Instance.GetDescriptorFromGuid(node["guid"]);
                    if (parent != null)
                    {
                        parent.Children.Add(descriptor);
                    }
                    if (descriptor is BarDescriptor)
                    {
                        node = descriptor.Node;
                    }
                    else
                    {
                        throw new InvalidOperationException("Object that is identified by the Guid " + node["guid"] + " is not of type Bar.");
                    }
                }
                return true;
            }
            return false;
        }
    }
}
